package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.Global;
import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import com.hzhr.iot.collect.service.helper.Device_IotDeviceServiceHelper;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDevicePutModel;
import com.hzhr.iot.device.sdk.service.Device_IotDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 集中器 集中器心跳周期查询
 * @author 48310
 *
 */
@Component
public class MessageProcessor79 implements MessageProcessor {
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	Device_IotDeviceService device_iotDeviceService;
	@Autowired
	Device_IotDeviceServiceHelper device_iotDeviceServiceHelper;
	@Autowired
	Global global;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		String code = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_CONCENTRATOR_NO));
		Integer heartPeriod = HzhrTypeUtil.object2integer(parsedMessageObj.get(MessageFieldKeyEnum.RESP_HEART_PERIOD));

		if (!StringUtils.isEmpty(code)) {
			IotDeviceModel iotDeviceModel = device_iotDeviceServiceHelper.getByCode(code);
			if (iotDeviceModel != null) {
				iotDeviceModel.setHeartPeriod(heartPeriod);
				IotDevicePutModel iotDevicePutModel = HzhrJsonUtil.convertObjectType(iotDeviceModel, IotDevicePutModel.class);
				Map<String ,String> header = new HashMap<>();
				header.put("token", global.getFirstToken());
				Map<String, Object> query = new HashMap<>();
				device_iotDeviceService.putById(header, iotDeviceModel.getId(), query, iotDevicePutModel);
			}
		}

		return null;
	}
}
